﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace GDI及数据库应用程序设计
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        int a = 0;
        bool b = false;

        private void 点ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            a = 1;
        }
        List<Point> lp = new List<Point>();
        List<Myline> ml = new List<Myline>();
        List<mypolygon> mp = new List<mypolygon>();
        Myline templine = new Myline();
        mypolygon tempmp = new mypolygon();

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (a == 1)
            {
                Point pt = new Point();
                pt.X = e.X;
                pt.Y = e.Y;
                lp.Add(pt);
                Invalidate();
            }
            if (a == 2)
            {
                templine = new Myline();
                templine.start.X = e.X;
                templine.start.Y = e.Y;
                //Invalidate();
                ml.Add(templine); 
            }
            if (a == 3 && e.Button == MouseButtons.Left)
            {
                Point pt = new Point();
                pt.X = e.X;
                pt.Y = e.Y;
                tempmp.pol.Add(pt);
                Invalidate();
                //mp.Add(tempmp);
                
            } if (a == 3 && e.Button == MouseButtons.Right)
            {
                mp.Add(tempmp);
                tempmp = new mypolygon();
                Invalidate();
            }
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //Graphics g = e.Graphics;
            if (a == 1)
            {
                Graphics g = e.Graphics;
                SolidBrush sb = new SolidBrush(Color.Black);
                foreach (Point pt in lp)
                {
                    g.FillEllipse(sb, pt.X, pt.Y, 4, 4);
                }
                sb.Dispose();
                g.Dispose();
            }
            if(a==2)
            {
                Graphics g = e.Graphics;
                Pen pen = new Pen(Color.Black, 2);
                foreach (Myline mline in ml)
                {
                    g.DrawLine(pen, mline.start.X, mline.start.Y, mline.end.X, mline.end.Y);
                }
                pen.Dispose();
                g.Dispose();
            }
            if (a == 3)
            {
                Graphics g = e.Graphics;
                SolidBrush sb = new SolidBrush(Color.Red);
                foreach (mypolygon mpl in mp)
                {
                    Point[] point = new Point[] { };
                    point = mpl.pol.ToArray();
                    g.FillPolygon(sb, point);
                }
                sb.Dispose();
                g.Dispose();
            }
        }
        class Myline
        {
            public Point start;
            public Point end;
            public Myline()
            {
                start = new Point();
                end = new Point();
            }
        }
        class mypolygon
        {
            public List<Point> pol;
            public mypolygon()
            {
                pol = new List<Point>();
            }
        }

        private void 线ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            a = 2;
        }
        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //string str;
            //str=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Access数据库\GDI+.mdb";
            //OleDbConnection mycon = new OleDbConnection();
            //mycon.ConnectionString = str;
            //mycon.Open();
            //if (mycon.State == ConnectionState.Open)
            //    MessageBox.Show("连接成功");
            if (a == 1)
            {
                string str;
                str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Access数据库\GDI+.mdb";
                OleDbConnection mycon = new OleDbConnection();
                mycon.ConnectionString = str;
                mycon.Open();
                OleDbCommand oc = new OleDbCommand();
                oc.Connection = mycon;
                //string strdel = "update zym set 点x=null and 点y=null";
                string strdel = "delete from zym where 点x and 点y";
                oc.CommandText = strdel;
                oc.ExecuteNonQuery();
                string[] arryx = new string[] { };
                string[] arryy = new string[] { };
                List<string> x = new List<string>();
                List<string> y = new List<string>();
                foreach (Point pt in lp)
                {
                    x.Add(pt.X.ToString());
                }
                foreach (Point pt in lp)
                {
                    y.Add(pt.Y.ToString());
                }
                arryx = x.ToArray();
                arryy = y.ToArray();
                for (int i = 1; i <= arryx.Length - 1; i++)
                {
                    string strtext = "insert into zym (点x,点y) values('";
                    strtext += arryx[i] + "','";
                    strtext += arryy[i] + "')";
                    oc.CommandText = strtext;
                    oc.ExecuteNonQuery();
                }
                mycon.Close();
                mycon.Dispose();
                //for (int i = 1; i <= arryy.Length - 1; i++)
                //{
                //    string strtext = "update zym set 点y ='";
                //    strtext += arryy[i] + "' where ID =";
                //    strtext += i.ToString();
                //    oc.CommandText = strtext;
                //    oc.ExecuteNonQuery();
                //}
                MessageBox.Show("保存成功");
            }
            if (a == 2)
            {
                string str;
                str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Access数据库\GDI+.mdb";
                OleDbConnection mycon = new OleDbConnection();
                mycon.ConnectionString = str;
                mycon.Open();
                OleDbCommand oc = new OleDbCommand();
                oc.Connection = mycon;
                string strdel = "delete from zym1 where startx ";
                oc.CommandText = strdel;
                oc.ExecuteNonQuery();
                string[] strstartx = new string[] { };
                string[] strstarty = new string[] { };
                string[] strendx = new string[] { };
                string[] strendy = new string[] { };
                List<string> startx = new List<string>();
                List<string> starty = new List<string>();
                List<string> endx = new List<string>();
                List<string> endy = new List<string>();
                foreach (Myline l in ml)
                {
                    startx.Add(l.start.X.ToString());
                    starty.Add(l.start.Y.ToString());
                }
                foreach (Myline l in ml)
                {
                    endx.Add(l.end.X.ToString());
                    endy.Add(l.end.Y.ToString());
                }
                strstartx = startx.ToArray();
                strstarty = starty.ToArray();
                strendx = endx.ToArray();
                strendy = endy.ToArray();
                for (int i = 1; i <= strstartx.Length - 1; i++)
                {
                    string strtext = "insert into zym1 (startx,starty,endx,endy) values('";
                    strtext += strstartx[i] + "','";
                    strtext += strstarty[i] + "','";
                    strtext += strendx[i] + "','";
                    strtext += strendy[i] + "')";
                    oc.CommandText = strtext;
                    oc.ExecuteNonQuery();
                }
                mycon.Close();
                mycon.Dispose();
                MessageBox.Show("保存成功");
            }
            if (a == 3)
            {
                string str;
                str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Access数据库\GDI+.mdb";
                OleDbConnection mycon = new OleDbConnection();
                mycon.ConnectionString = str;
                mycon.Open();
                OleDbCommand oc = new OleDbCommand();
                oc.Connection = mycon;
                string strdel = "delete from zym2 where listx ";
                oc.CommandText = strdel;
                oc.ExecuteNonQuery();
                foreach (mypolygon mpl in mp)
                {
                    Point[] pt = new Point[] { };
                    pt = mpl.pol.ToArray();
                    string[] strx = new string[] { };
                    string[] stry = new string[] { };
                    string str1 = null;
                    string str2 = null;
                    List<string> x = new List<string>();
                    List<string> y = new List<string>();
                    for (int i = 0; i <= pt.Length - 1; i++)
                    {
                        x.Add(pt[i].X.ToString());
                        y.Add(pt[i].Y.ToString());
                    }
                    strx = x.ToArray();
                    stry = y.ToArray();
                        //string strtext = "insert into zym2 (listx,listy) values('";
                        //strtext += strx[i] + "','";
                        //strtext += stry[i] + "')";
                    for (int i = 0; i <= strx.Length - 1; i++)
                    {
                        str1 += strx[i] + ",";
                        str2 += stry[i] + ",";
                    }
                    str1=str1.Remove(str1.Length-1);
                    str2=str2.Remove(str2.Length-1);
                    string strtext = "insert into zym2 (listx,listy) values('";
                    strtext += str1 + "','";
                    strtext += str2 + "')";
                    oc.CommandText = strtext;
                    oc.ExecuteNonQuery();
                    
                }
                mycon.Close();
                mycon.Dispose();
                MessageBox.Show("保存成功");
            }
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void 信息查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            b = true;
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {

                if (b == true)
                {
                    this.toolTip1.SetToolTip(this, e.X + "\t" + e.Y);
                }
  
        }

        private void 点ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            string str;
            str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Access数据库\GDI+.mdb";
            OleDbConnection mycon = new OleDbConnection();
            mycon.ConnectionString = str;
            mycon.Open();
            OleDbCommand oc = new OleDbCommand();
            string strtext = "select * from zym";
            oc.Connection = mycon;
            oc.CommandText = strtext;
            OleDbDataReader myreadr = oc.ExecuteReader();
            //myreadr = oc.ExecuteReader();
            List<Point> templp = new List<Point>();
            //Point temppt =new Point();
            while (myreadr.Read())
            {
                Point temppt = new Point();
                temppt.X = Convert.ToInt32(myreadr[1]);
                temppt.Y = Convert.ToInt32(myreadr[2]);
                templp.Add(temppt);
            }
            mycon.Close();
            mycon.Dispose();

            Graphics g = this.CreateGraphics();
            SolidBrush sb = new SolidBrush(Color.Black);
            foreach (Point pt in templp)
            {
                g.FillEllipse(sb, pt.X, pt.Y, 4, 4);
            }
            sb.Dispose();
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (a == 2)
            {
                templine.end.X = e.X;
                templine.end.Y = e.Y;
                ml.Add(templine);
                Invalidate();

            }
        }

        private void 线ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            string str;
            str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Access数据库\GDI+.mdb";
            OleDbConnection mycon = new OleDbConnection();
            mycon.ConnectionString = str;
            mycon.Open();
            OleDbCommand oc = new OleDbCommand();
            string strtext = "select * from zym1";
            oc.Connection = mycon;
            oc.CommandText = strtext;
            OleDbDataReader myreadr = oc.ExecuteReader();
            List<Myline> templm = new List<Myline>();
            while (myreadr.Read())
            {
                Myline line = new Myline();
                line.start.X = Convert.ToInt32(myreadr[1]);
                line.start.Y = Convert.ToInt32(myreadr[2]);
                line.end.X = Convert.ToInt32(myreadr[3]);
                line.end.Y = Convert.ToInt32(myreadr[4]);
                templm.Add(line);
            }
            Graphics g = this.CreateGraphics();
            Pen pen = new Pen(Color.Black, 2);
            foreach (Myline mline in templm)
            {
                g.DrawLine(pen, mline.start.X, mline.start.Y, mline.end.X, mline.end.Y);
            }
            pen.Dispose();
            g.Dispose();
        }

        private void 画布清空ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Invalidate();
        }

        private void 面ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            a = 3;
        }

        private void 面ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            string str;
            str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Access数据库\GDI+.mdb";
            OleDbConnection mycon = new OleDbConnection();
            mycon.ConnectionString = str;
            mycon.Open();
            OleDbCommand oc = new OleDbCommand();
            oc.Connection = mycon;
            string strtext = "select * from zym2";
            oc.CommandText = strtext;
            OleDbDataReader myreadr = oc.ExecuteReader();
            List<string> lsx = new List<string>();
            List<string> lsy = new List<string>();
            List<mypolygon> mypol = new List<mypolygon>();
            string[] srx = new string[] { };
            string[] sry = new string[] { };
            while(myreadr.Read())
            {
                string sx,sy;
                sx = myreadr[0].ToString();
                sy = myreadr[1].ToString();
                lsx.Add(sx);
                lsy.Add(sy);
            }
            for (int i = 0; i <= lsx.Count - 1; i++)
            {
                srx = lsx[i].Split(',');
                sry = lsy[i].Split(',');
                mypolygon mpl = new mypolygon();
                for (int j = 0; j <= srx.Length - 1; j++)
                {
                    Point pt = new Point();
                    pt.X = Convert.ToInt32(srx[j]);
                    pt.Y = Convert.ToInt32(sry[j]);
                    mpl.pol.Add(pt);
                }
                mypol.Add(mpl);
            }
            Graphics g = this.CreateGraphics();
            SolidBrush sb = new SolidBrush(Color.Red);
            foreach (mypolygon m in mypol)
            {
                Point[] pt = new Point[] { };
                pt = m.pol.ToArray();
                g.FillPolygon(sb, pt);
            }
            sb.Dispose();
            g.Dispose();
        }

    }
}
